package com.jindu.producer.system.controller;

import com.jindu.kernel.message.MailSend;
import com.jindu.kernel.message.SlideVerification;
import com.jindu.kernel.result.JsonResult;
import com.jindu.kernel.result.StatusCode;
import com.jindu.kernel.utils.DateUtils;
import com.jindu.kernel.utils.IdGenerator;
import com.jindu.kernel.utils.RandomUtil;
import com.jindu.producer.system.config.MessageSend;
import com.jindu.producer.system.jpa.EmailVerificationJpa;
import com.jindu.producer.system.model.SEmailVerificationEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.sql.Timestamp;
import java.util.HashMap;

/**
 * @author jarvis
 * @date 2018/2/21
 */
@Api(description = "工具类及验证码管理", tags = "UtilsController", basePath = "/email")
@RestController
@RequestMapping(value = "/email",produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class EmailVerificationController {

    @Autowired
    EmailVerificationJpa emailVerificationJpa;


    @Autowired
    MessageSend messageSend;

    @ApiOperation(value = "根据用户的email地址，发送邮件验证码，并写入验证码的数据库表", notes = "返回是否发送成功的信息，用于注册时email验证")
    @RequestMapping(value = "sendVerificationCode",method = RequestMethod.POST)
    public JsonResult<HashMap<String,Object>> sendVerificationCode(@RequestParam String email,
                                                                   @RequestParam String lang,
                                                                   @RequestParam String sessionId,
                                                                   @RequestParam String sig,
                                                                   @RequestParam String token,
                                                                   @RequestParam String scene) throws Exception{

        HashMap<String,Object> codeMessage=new HashMap<>();
        SlideVerification sv=new SlideVerification();
        if(sv.verificationSlide(sessionId,sig,token,scene)){

            sendMailByAliyunIAcs(email,lang);
            codeMessage.put(StatusCode.CREATED.getCode().toString(),StatusCode.CREATED.getMeaning());
            return JsonResult.getFailureResult(StatusCode.OK,codeMessage);

        }else {
            codeMessage.put(StatusCode.SlideFailure.getCode().toString(),StatusCode.SlideFailure.getMeaning());
            return JsonResult.getResult(StatusCode.INVALID_REQUEST,codeMessage);
        }

    }

    public SEmailVerificationEntity sendMailByAliyunIAcs(String email, String lang) throws Exception{

        Integer emailCode= RandomUtil.getNum(100000,999999);
        SEmailVerificationEntity emailVerificationEntity=new SEmailVerificationEntity();

        emailVerificationEntity.setId(IdGenerator.uuid());
        emailVerificationEntity.setVerification(emailCode.toString());
        emailVerificationEntity.setEmail(email);
        emailVerificationEntity.setType(0);
        emailVerificationEntity.setStatus(1);
        emailVerificationEntity.setCreateTime(new Timestamp(DateUtils.getCurrentTime()));
        SEmailVerificationEntity entity=emailVerificationJpa.saveAndFlush(emailVerificationEntity);

        if(entity!=null){
            MailSend mailSend=new MailSend();
            /**
             * 需设置完善的邮件内容
             */
            String title,body;
            switch (lang){
                case "cn":
                    title=messageSend.getEmailRegisterTitleCn();
                    body=messageSend.getEmailRegisterBodyCn(emailCode.toString());
                    break;
                case "en":
                    title=messageSend.getEmailRegisterTitleEn();
                    body=messageSend.getEmailRegisterBodyEn(emailCode.toString());
                    break;
                default:
                    title=messageSend.getEmailRegisterTitleEn();
                    body=messageSend.getEmailRegisterBodyEn(emailCode.toString());
            }

            mailSend.MailSend(email,title,body);
        }
        return entity;
    }

}
